// pages/allProject/allProject.js
const db = wx.cloud.database();
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allProject: [],
    showProject: [],
    currentTag: '全部',
    tags: [{
      tagName: '全部'
    }, {
      tagName: '生产'
    }, {
      tagName: '农产改善'
    }, {
      tagName: '农产加工'
    }, {
      tagName: '非盈利项目'
    }, {
      tagName: '综合'
    }]
  },
  timeDescSort(arr) {
    for (let i = 0; i < arr.length - 1; i++) {
      for (let j = 0; j < arr.length - i - 1; j++) {
        if (arr[j].publishTime < arr[j + 1].publishTime) {
          let tmp = arr[j];
          arr[j] = arr[j + 1];
          arr[j + 1] = tmp;
        }
      }
    }
    return arr
  },
  currentTagChange(e) {
    let that = this;
    console.log(e.currentTarget.dataset.currenttag.tagName);
    this.setData({
      currentTag: e.currentTarget.dataset.currenttag.tagName
    })
    let tagName = e.currentTarget.dataset.currenttag.tagName;


    switch (tagName) {
      case '全部': {
        that.setData({
          showProject: that.data.allProject
        });
        console.log(that.data.showProject);
        break;
      }
      case '生产': {
        let tmp = new Array(0);
        that.data.allProject.forEach(element => {
          if (element.type == "生产") {
            tmp = tmp.concat(element);
          }
        });
        that.setData({
          showProject: tmp
        });
        console.log(that.data.showProject);
        break;
      }
      case '农产改善': {
        let tmp = new Array(0);
        that.data.allProject.forEach(element => {
          if (element.type == "农产改善") {
            tmp = tmp.concat(element);
          }
        });
        that.setData({
          showProject: tmp
        });
        console.log(that.data.showProject);
        break;
      }
      case '农产加工': {
        let tmp = new Array(0);
        that.data.allProject.forEach(element => {
          if (element.type == "农产加工") {
            tmp = tmp.concat(element);
          }
        });
        that.setData({
          showProject: tmp
        });
        console.log(that.data.showProject);
        break;
      }
      case '非盈利项目': {
        let tmp = new Array(0);
        that.data.allProject.forEach(element => {
          if (element.type == "非盈利项目") {
            tmp = tmp.concat(element);
          }
        });
        that.setData({
          showProject: tmp
        });
        console.log(that.data.showProject);
        break;
      }
      case '综合': {
        let tmp = new Array(0);
        that.data.allProject.forEach(element => {
          if (element.type == "综合") {
            tmp = tmp.concat(element);
          }
        });
        that.setData({
          showProject: tmp
        });
        console.log(that.data.showProject);
        break;
      }
    }


  },
  navBack() {
    wx.navigateBack({
      delta: 1,
    })
  },
  navToSearchProject() {
    wx.navigateTo({
      url: '/pages/searchProject/searchProject',
    })
  },
  navToProjectDetail(e) {
    // console.log(e.currentTarget.dataset.project_detail);
    app.navTmpInfo = e.currentTarget.dataset.project_detail;
    wx.navigateTo({
      url: '/pages/project_detail/project_detail',
    })
  },


  // ----------------------------------------------------------------------
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading({
      title: '获取数据中',
    })
    let that = this;
    // 调用突破20条限制的数据查询云函数
    wx.cloud.callFunction({
      name: 'getAllRecord',
      data: {
        collectionName: 'projects'
      },
      success: res => {
        let afterSortResult = that.timeDescSort(res.result.all);
        that.setData({
          allProject: afterSortResult
        })
        this.setData({
          showProject: that.data.allProject
        })
        console.log(that.data.showProject);
      },
      complete: () => {
        wx.hideLoading()
      }
    });


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})